"""
规则管理模块
负责加载和管理违规检测规则
"""

import json
import logging

logger = logging.getLogger(__name__)

class RuleManager:
    """规则管理器"""

    def __init__(self, rule_file: str = 'rule-definitions.json'):
        self.rule_file = rule_file
        self.rules = {}

    def load_rules(self) -> dict:
        """加载规则定义"""
        try:
            with open(self.rule_file, 'r') as f:
                data = json.load(f)
                rules_dict = {}
                for rule in data['rules']:
                    rules_dict[rule['rule_name']] = {
                        'keywords': rule['keywords'],
                        'description': rule['description']
                    }
                logger.info(f"成功加载 {len(rules_dict)} 个规则")
                return rules_dict
        except Exception as e:
            logger.error(f"加载规则失败: {str(e)}")
            # 返回默认规则
            return {
                "友商品牌": {"keywords": ["汽车之家", "易车"], "description": "检测图片中是否包含友商品牌名称"},
                "敏感内容": {"keywords": ["暴力", "色情", "赌博"], "description": "检测图片中是否包含敏感内容"}
            }

    def get_rules(self) -> dict:
        """获取当前规则"""
        if not self.rules:
            self.rules = self.load_rules()
        return self.rules

# 全局规则管理器实例
rule_manager = RuleManager()
RULES = rule_manager.get_rules()
